<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Todo;
use App\Models\AdminUser;
use App\Traits\ControllerHasTab;
use App\Support\Data;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Tab;


class ProcessedController extends AdminController
{
    use ControllerHasTab;

    public function tab(): Row
    {
        $row = new Row();
        $tab = new Tab();
        $tab->addLink(Data::icon('record') . '待处理事项', admin_route('todo.list.index'));
        $tab->add(Data::icon('track') . '已处理事项', $this->renderGrid(), true);
        // $tab->add(Data::icon('track') . '已处理事项', $this->renderGrid(), true, 2);
        $row->column(12, $tab);
        return $row;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Todo(), function (Grid $grid) {

            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            // $grid->disableViewButton();
            $grid->disableCreateButton();

            $grid->column('id')->sortable();
            $grid->column('todo_type')->display(function ($id) {
                return Data::getNoticeType($id);
            });
            $grid->column('content')->limit(30);
            $grid->column('admin_id')->display(function ($id) {
                return AdminUser::find($id)->username ?? '';
            })->label();
            $grid->column('status')->using(['0' => '未处理', '1' => '已处理'])->badge([
                0 => 'danger',
                1 => 'success',
            ]);
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at');

            $grid->model()->orderBy('id', 'desc');
            $grid->model()->where('status', 1);
            // $grid->filter(function (Grid\Filter $filter) {
            //     $filter->equal('id');
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Todo(), function (Show $show) {
            // $show->disableListButton();
            $show->disableDeleteButton();
            $show->disableEditButton();
            $show->field('id');
            $show->field('todo_type')->as(function ($id) {
                return Data::getNoticeType($id);
            });;
            $show->field('content');
            $show->field('admin_id')->as(function ($id) {
                return AdminUser::find($id)->username ?? '';
            })->label();
            $show->field('status')->using(['0' => '未处理', '1' => '已处理'])->dot([
                0 => 'danger',
                1 => 'success',
            ]);
            $show->field('over_at', '完成时间');
            $show->field('desc', '完成描述');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Todo(), function (Form $form) {
            $form->display('id');
            $form->text('todo_type');
            $form->text('content');
            $form->text('admin_id');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
